{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 2 Series"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.496714\n",
      "1   -0.138264\n",
      "2    0.647689\n",
      "3    1.523030\n",
      "4   -0.234153\n",
      "5   -0.234137\n",
      "6    1.579213\n",
      "7    0.767435\n",
      "8   -0.469474\n",
      "9    0.542560\n",
      "dtype: float64\n",
      "<class 'pandas.core.series.Series'>\n",
      "float64\n",
      "--------------------------------------------------\n",
      "[ 0.49671415 -0.1382643   0.64768854  1.52302986 -0.23415337 -0.23413696\n",
      "  1.57921282  0.76743473 -0.46947439  0.54256004]\n",
      "<class 'numpy.ndarray'>\n",
      "float64\n",
      "--------------------------------------------------\n",
      "RangeIndex(start=0, stop=10, step=1)\n",
      "<class 'pandas.core.indexes.range.RangeIndex'>\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "np.random.seed(42)\n",
    "\n",
    "# 生成一个Series\n",
    "arr = np.random.randn(10)\n",
    "ser_obj = pd.Series(arr) #默认索引是0-9\n",
    "#ser_obj = pd.Series(range(1, 6))\n",
    "print(ser_obj) #打印输出会带有类型\n",
    "print(type(ser_obj))\n",
    "print(ser_obj.dtype)\n",
    "print('-'*50)\n",
    "\n",
    "# 获取数据\n",
    "print(ser_obj.values)  #values实际是ndarray\n",
    "print(type(ser_obj.values)) #类型是ndarray\n",
    "print(ser_obj.values.dtype) # 同上\n",
    "print('-'*50)\n",
    "\n",
    "# 获取索引\n",
    "print(ser_obj.index)  #内部自带的类型--RangeIndex\n",
    "print(type(ser_obj.index)) #类型是RangeIndex\n",
    "print(ser_obj.index.dtype)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T01:40:33.241949800Z",
     "start_time": "2024-05-01T01:40:33.016811700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4967141530112327\n"
     ]
    },
    {
     "data": {
      "text/plain": "0.5425600435859647"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(ser_obj[0]) \n",
    "ser_obj[9]\n",
    "# 访问不存在的索引下标会报keyerror"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T01:40:33.243944100Z",
     "start_time": "2024-05-01T01:40:33.033179100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.993428\n",
      "1   -0.276529\n",
      "2    1.295377\n",
      "3    3.046060\n",
      "4   -0.468307\n",
      "5   -0.468274\n",
      "6    3.158426\n",
      "7    1.534869\n",
      "8   -0.938949\n",
      "9    1.085120\n",
      "dtype: float64\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6    False\n",
      "7    False\n",
      "8    False\n",
      "9    False\n",
      "dtype: bool\n",
      "2    0.647689\n",
      "3    1.523030\n",
      "6    1.579213\n",
      "7    0.767435\n",
      "9    0.542560\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(ser_obj * 2)  #元素级乘法\n",
    "print(ser_obj > 15) #返回一个bool序列\n",
    "print(ser_obj[ser_obj > 0.5]) # 筛选"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T01:40:33.243944100Z",
     "start_time": "2024-05-01T01:40:33.050719800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001    17.8\n",
      "2005     abc\n",
      "2003    16.5\n",
      "dtype: object\n",
      "object\n",
      "--------------------------------------------------\n",
      "[17.8 'abc' 16.5]\n",
      "object\n",
      "--------------------------------------------------\n",
      "Index([2001, 2005, 2003], dtype='int64')\n",
      "<class 'pandas.core.indexes.base.Index'>\n",
      "--------------------------------------------------\n",
      "17.8\n"
     ]
    }
   ],
   "source": [
    "#字典变为series，索引是字典的key，value是字典的value，感受非默认索引\n",
    "\n",
    "year_data = {2001: 17.8, 2005: \"abc\", 2003: 16.5}\n",
    "ser_obj2 = pd.Series(year_data)\n",
    "print(ser_obj2)\n",
    "print(ser_obj2.dtype)\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(ser_obj2.values)\n",
    "print(ser_obj2.values.dtype)\n",
    "print('-'*50)\n",
    "\n",
    "print(ser_obj2.index)\n",
    "print(type(ser_obj2.index))\n",
    "print('-'*50)\n",
    "\n",
    "print(ser_obj2[2001])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T01:40:33.243944100Z",
     "start_time": "2024-05-01T01:40:33.066676900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 可以指定Series名、索引列名"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "temp\n",
      "year1\n",
      "--------------------------------------------------\n",
      "year1\n",
      "2001    17.8\n",
      "2005     abc\n",
      "2003    16.5\n",
      "Name: temp, dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(ser_obj2.name) #Series名字\n",
    "print(ser_obj2.index.name)  #索引名字\n",
    "\n",
    "ser_obj2.name = 'temp'\n",
    "ser_obj2.index.name = 'year1'\n",
    "#ser_obj2.values.name =  # 没有这玩意\n",
    "print('-'*50)\n",
    "\n",
    "print(ser_obj2.head())  # 指定显示前几行，默认前5行\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T02:34:09.828368400Z",
     "start_time": "2024-05-01T02:34:09.820369300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3 DataFrame"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1   2   3\n",
      "0  0  1   2   3\n",
      "1  4  5   6   7\n",
      "2  8  9  10  11\n",
      "--------------------------------------------------\n",
      "[[ 0.49671415 -0.1382643   0.64768854  1.52302986 -0.23415337 -0.23413696]\n",
      " [ 1.57921282  0.76743473 -0.46947439  0.54256004 -0.46341769 -0.46572975]\n",
      " [ 0.24196227 -1.91328024 -1.72491783 -0.56228753 -1.01283112  0.31424733]\n",
      " [-0.90802408 -1.4123037   1.46564877 -0.2257763   0.0675282  -1.42474819]]\n",
      "--------------------------------------------------\n",
      "          0         1         2        3         4         5\n",
      "0  0.496714 -0.138264  0.647689  1.52303 -0.234153 -0.234137\n",
      "1  1.579213  0.767435 -0.469474  0.54256 -0.463418 -0.465730\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "np.random.seed(42)\n",
    "\n",
    "# 通过ndarray构建DataFrame\n",
    "t = pd.DataFrame(np.arange(12).reshape((3,4))) #默认索引是0-2\n",
    "print(t)\n",
    "print('-'*50)\n",
    "\n",
    "array = np.random.randn(4,6)\n",
    "print(array)\n",
    "print('-'*50)\n",
    "\n",
    "df_obj = pd.DataFrame(array)\n",
    "print(df_obj.head(2)) # 也是默认前5行，列不管"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T02:40:20.533174600Z",
     "start_time": "2024-05-01T02:40:20.507228200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    4\n",
      "1    5\n",
      "2    6\n",
      "3    7\n",
      "Name: 1, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "print(t.loc[1]) #单独把某一行取出来,类型是series"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T02:57:27.985297200Z",
     "start_time": "2024-05-01T02:57:27.953383800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name   age      tel\n",
      "0  xiaohong  32.0  10010.0\n",
      "1  xiaogang   NaN  10000.0\n",
      "2  xiaowang  22.0      NaN\n",
      "\n",
      "name     object\n",
      "age     float64\n",
      "tel     float64\n",
      "dtype: object\n",
      "--------------------------------------------------\n",
      "[['xiaohong' 32.0 10010.0]\n",
      " ['xiaogang' nan 10000.0]\n",
      " ['xiaowang' 22.0 nan]]\n",
      "<class 'numpy.ndarray'>\n",
      "[32.0 10010.0]\n",
      "object\n",
      "object\n",
      "--------------------------------------------------\n",
      "RangeIndex(start=0, stop=3, step=1)\n"
     ]
    }
   ],
   "source": [
    "# 列表套字典  变df\n",
    "d2 =[{\"name\" : \"xiaohong\" ,\"age\" :32,\"tel\" :10010},\n",
    "     { \"name\": \"xiaogang\" ,\"tel\": 10000} ,\n",
    "     {\"name\":\"xiaowang\" ,\"age\":22}]\n",
    "\n",
    "df6=pd.DataFrame(d2)\n",
    "print(df6) #缺失值会用NaN填充\n",
    "print()\n",
    "print(df6.dtypes)\n",
    "print('-'*50)\n",
    "\n",
    "print(df6.values)\n",
    "print(type(df6.values)) #ndarray\n",
    "print(df6.values[0])\n",
    "print(df6.values[0].dtype)\n",
    "print(df6.values.dtype)\n",
    "print('-'*50)\n",
    "\n",
    "print(df6.index)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T03:04:07.021286500Z",
     "start_time": "2024-05-01T03:04:07.007324400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 数据列广播机制、指定索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3    2.0\n",
      "4    2.0\n",
      "5    2.0\n",
      "6    2.0\n",
      "dtype: float32\n"
     ]
    }
   ],
   "source": [
    "print(pd.Series(2, index=list(range(3,7)),dtype='float32'))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T03:04:28.921838500Z",
     "start_time": "2024-05-01T03:04:28.894876600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n",
      "--------------------------------------------------\n",
      "Index([0, 1, 2, 3], dtype='int64')\n",
      "--------------------------------------------------\n",
      "Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')\n",
      "--------------------------------------------------\n",
      "A             int64\n",
      "B    datetime64[ns]\n",
      "C           float32\n",
      "D             int32\n",
      "E            object\n",
      "F            object\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "#df中不同列可以是不同的数据类型,同一列必须是一个数据类型\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "\n",
    "print(df_obj2.index) #行索引,重点\n",
    "# df_obj2.index[0]=2  不可以单独修改某个索引值\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(df_obj2.columns) #列索引，重点\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(df_obj2.dtypes) # 特征的数据类型"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T03:06:24.755454Z",
     "start_time": "2024-05-01T03:06:24.747514700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   A         B         C         D\n",
      "2013-01-01  0.822545 -1.220844  0.208864 -1.959670\n",
      "2013-01-02 -1.328186  0.196861  0.738467  0.171368\n",
      "2013-01-03 -0.115648 -0.301104 -1.478522 -0.719844\n",
      "2013-01-04 -0.460639  1.057122  0.343618 -1.763040\n",
      "2013-01-05  0.324084 -0.385082 -0.676922  0.611676\n",
      "2013-01-06  1.031000  0.931280 -0.839218 -0.309212\n",
      "--------------------------------------------------\n",
      "DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',\n",
      "               '2013-01-05', '2013-01-06'],\n",
      "              dtype='datetime64[ns]', freq='D')\n",
      "datetime64[ns]\n",
      "<class 'pandas.core.indexes.datetimes.DatetimeIndex'>\n",
      "                   A         B         C         D\n",
      "2013-01-01  0.822545 -1.220844  0.208864 -1.959670\n",
      "2013-01-02 -1.328186  0.196861  0.738467  0.171368\n",
      "2013-01-03 -0.115648 -0.301104 -1.478522 -0.719844\n",
      "2013-01-04 -0.460639  1.057122  0.343618 -1.763040\n",
      "2013-01-05  0.324084 -0.385082 -0.676922  0.611676\n",
      "2013-01-06  1.031000  0.931280 -0.839218 -0.309212\n"
     ]
    }
   ],
   "source": [
    "# 感受日期,初始化df，设置行索引，列索引\n",
    "dates = pd.date_range('20130101', periods=6) #默认freq='D'，即天\n",
    "\n",
    "df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))\n",
    "print(df)\n",
    "print('-'*50)\n",
    "\n",
    "print(df.index)\n",
    "print(df.index.dtype)\n",
    "print(type(df.index))\n",
    "print(df)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T01:40:33.268912800Z",
     "start_time": "2024-05-01T01:40:33.213028600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "[[1 Timestamp('2019-09-26 00:00:00') 1.0 1 'Python' 'wangdao']\n",
      " [1 Timestamp('2019-09-26 00:00:00') 1.0 2 'Java' 'wangdao']\n",
      " [1 Timestamp('2019-09-26 00:00:00') 1.0 3 'C++' 'wangdao']\n",
      " [1 Timestamp('2019-09-26 00:00:00') 1.0 4 'C' 'wangdao']]\n",
      "--------------------------------------------------\n",
      "0   2019-09-26\n",
      "1   2019-09-26\n",
      "2   2019-09-26\n",
      "3   2019-09-26\n",
      "Name: B, dtype: datetime64[ns]\n",
      "<class 'pandas.core.series.Series'>\n",
      "--------------------------------------------------\n",
      "A                      1\n",
      "B    2019-09-26 00:00:00\n",
      "C                    1.0\n",
      "D                      1\n",
      "E                 Python\n",
      "F                wangdao\n",
      "Name: 0, dtype: object\n"
     ]
    }
   ],
   "source": [
    "#取数据\n",
    "print(df_obj2)\n",
    "print(type(df_obj2))\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(df_obj2.values)\n",
    "print(\"-\"*50)\n",
    "\n",
    "#pd中使用索引名来取某一行，或者列\n",
    "print(df_obj2['B'])\n",
    "#把df的某一列取出来是series\n",
    "print(type(df_obj2['B']))\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(df_obj2.loc[0]) #取第一行\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T03:16:02.560390Z",
     "start_time": "2024-05-01T03:16:02.544510700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F  G\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao  5\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao  6\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao  7\n",
      "3  1 2019-09-26  1.0  4       C  wangdao  8\n"
     ]
    }
   ],
   "source": [
    "#增加列数据，列名是自定义的\n",
    "df_obj2['G'] = df_obj2['D'] + 4\n",
    "print(df_obj2.head())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T01:40:33.269911300Z",
     "start_time": "2024-05-01T01:40:33.239988100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n"
     ]
    }
   ],
   "source": [
    "# 删除列\n",
    "del(df_obj2['G'])\n",
    "print(df_obj2.head())\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T01:40:33.460365700Z",
     "start_time": "2024-05-01T01:40:33.256951800Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
